{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-6 (nowDateTime):\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py\", line 1045, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"c:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\ipykernel\\ipkernel.py\", line 766, in run_closure\n",
      "    _threading_Thread_run(self)\n",
      "  File \"c:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\threading.py\", line 982, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_15020\\17740209.py\", line 66, in nowDateTime\n",
      "  File \"c:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\tkinter\\__init__.py\", line 590, in get\n",
      "    value = self._tk.globalgetvar(self._name)\n",
      "            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "RuntimeError: main thread is not in main loop\n"
     ]
    }
   ],
   "source": [
    "import tkinter\n",
    "import threading\n",
    "import time\n",
    "import datetime\n",
    "\n",
    "app = tkinter.Tk()\n",
    "# 不显示标题栏\n",
    "app.overrideredirect(True)\n",
    "# 半透明窗体\n",
    "app.attributes('-alpha', 0.9)\n",
    "# 窗口总是在顶端显示\n",
    "app.attributes('-topmost', 1)\n",
    "# 设置初始大小与位置\n",
    "app.geometry('110x25+100+100')\n",
    "labelDateTime = tkinter.Label(app)\n",
    "labelDateTime.pack(fill=tkinter.BOTH, expand=tkinter.YES)\n",
    "labelDateTime.config(bg='gray')\n",
    "# 变量X和Y用来记录鼠标左键按下的位置\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "# 表示窗口是否可拖动的变量\n",
    "canMore = tkinter.IntVar(value=0)\n",
    "# 表示是否仍在运行的变量\n",
    "still = tkinter.IntVar(value=1)\n",
    "\n",
    "def onLeftButtonDown(event):\n",
    "    # 开始拖动时增加透明度\n",
    "    app.attributes('-alpha', 0.4)\n",
    "    # 鼠标左键按下，记录当前位置\n",
    "    X.set(event.x)\n",
    "    Y.set(event.y)\n",
    "    # 标记窗口可拖动\n",
    "    canMore.set(1)\n",
    "# 绑定鼠标左键单击事件处理函数\n",
    "labelDateTime.bind('<Button-1>', onLeftButtonDown)\n",
    "\n",
    "def onLeftButtonUp(event):\n",
    "    # 停止拖动时恢复透明度\n",
    "    app.attributes('-alpha', 0.9)\n",
    "    # 鼠标左键释放，标记窗口不可拖动\n",
    "    canMore.set(0)\n",
    "# 绑定鼠标左键释放事件处理函数\n",
    "labelDateTime.bind('<ButtonRelease-1>', onLeftButtonUp)\n",
    "\n",
    "def onLeftButtonMove(event):\n",
    "    if canMore.get() == 0:\n",
    "        return\n",
    "    # 重新计算并修改窗口的新位置\n",
    "    newX = app.winfo_x() + (event.x - X.get())\n",
    "    newY = app.winfo_y() + (event.y - Y.get())\n",
    "    g = '110x25+' + str(newX) + '+' + str(newY)\n",
    "    app.geometry(g)\n",
    "# 绑定鼠标左键移动事件处理函数\n",
    "labelDateTime.bind('<B1-Motion>', onLeftButtonMove)\n",
    "\n",
    "def onRightButtonDown(event):\n",
    "    still.set(0)\n",
    "    t.join(0.2)\n",
    "    # 关闭窗口\n",
    "    app.destroy()\n",
    "# 绑定鼠标右键单击事件处理函数\n",
    "labelDateTime.bind('<Button-3>', onRightButtonDown)\n",
    "\n",
    "# 显示当前时间的线程函数\n",
    "def nowDateTime():\n",
    "    while still.get() == 1:\n",
    "        # 获取当前的日期时间\n",
    "        now = datetime.datetime.now()\n",
    "        s = str(now.year) + '-' + str(now.month) + '-' + str(now.day) + ' ' + str(now.hour) + ':' + str(now.minute) + ':' + str(now.second)\n",
    "        # 显示当前时间\n",
    "        labelDateTime['text'] = s\n",
    "        # 0.5秒刷新一次日期时间\n",
    "        time.sleep(0.5)\n",
    "# 创建并启动线程\n",
    "t = threading.Thread(target=nowDateTime)\n",
    "t.daemon = True\n",
    "t.start()\n",
    "\n",
    "app.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
